//
//  KindsCollectionView.swift
//  fsapp
//
//  Created by 刘勇 on 2018/2/21.
//  Copyright © 2018年 huangshengjie. All rights reserved.
//

import UIKit
import Alamofire

extension FillLearningViewController:UICollectionViewDelegate,UICollectionViewDataSource{
    
//
//    override func viewDidDisappear(_ animated: Bool) {
//        super.viewDidDisappear(animated)
//        self.ChildType.removeAll()
//    }
    
    
    
    
    func getKind(){
        let url = "http://dahan-api.heiyou.net/index.php/home/index/getChildType"
        let paramdic = ["parent":type]
        Alamofire.request( url, method: .post, parameters: paramdic).responseJSON{ response
            in
            
            if let al = response.response{
                
            }else{
           
            }
            
            //response serialization
            if let JSON2 = response.result.value {
                let js = JSON(JSON2)
                print(js)
                if js["status"] == 200{
                    if js["result"].count != 0{
                        var nw:[
                            String:[String]] = ["id":[],"name":[]]
                        for i in 0...js["result"].count-1{
                            if let m =  js["result"][i]["name"].string{
                               nw["name"]?.append(m)
                                nw["id"]?.append(js["result"][i]["id"].string!)
                            }
                        }
                         self.ChildType=nw
                    }
                    
                }
                
                self.collectionView?.reloadData()
               
                
                
            }
        }
        
        
    }
    
    
    func setCollectionView(){
        self.getKind()
        layout.estimatedItemSize = CGSize(width:getWidth(width: 100),height:getHeight(15))
        layout.minimumInteritemSpacing = getWidth(width: 30)
        layout.minimumLineSpacing = getHeight(10)
        layout.sectionInset = UIEdgeInsetsMake(getHeight(8), getWidth(width: 14), getHeight(8), getWidth(width: 14))
        collectionView = UICollectionView(frame:CGRect(x:0,y:getHeight(65),width:SCREEN_WIDTH,height:getHeight(65)),collectionViewLayout:layout)
        //self.collectionView?.collectionViewLayout = layout
        //self.collectionView.frame = self.view.frame
        self.collectionView?.register(TextLabelCell.self, forCellWithReuseIdentifier: "cell")
        self.collectionView?.delegate = self
        self.collectionView?.dataSource = self
        self.collectionView?.backgroundColor = UIColor.white
       collectionView?.allowsMultipleSelection = true
        collectionView?.isScrollEnabled = false
        
       // self.view.addSubview((collectionView)!)
    }
    
    
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.ChildType["name"]!.count//3//self.dataBase["nation_id"]!.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
       // let cell = TextLabelCell.init(frame: CGRect(x:getWidth(width: 0),y:getHeight(0),width:getWidth(width: 96),height:getHeight(40)))
            
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TextLabelCell
        
        cell.texts.text = self.ChildType["name"]?[indexPath.row]
        cell.layer.cornerRadius = 10
        cell.layer.masksToBounds = true
        cell.layer.borderWidth = getHeight(2)
        cell.layer.borderColor = title2color.cgColor
        
        if selectArr==nil{
            cell.texts.textColor = title2color

            cell.backgroundColor = UIColor.white
        }else if indexPath.row == selectArr{
            cell.backgroundColor = naviColor
            cell.texts.textColor = UIColor.white
            cell.isSelected = true
        }else{
             cell.texts.textColor = title2color
            cell.backgroundColor = UIColor.white
        }
        
        
        // cell.backgroundColor = naviColor
       // let Cornerlabel = UILabel.init(frame: CGRect(x:getWidth(width: 0),y:getHeight(0),width:getWidth(width: 96),height:getHeight(40)))
        //Cornerlabel.backgroundColor = naviColor
       // Cornerlabel.layer.cornerRadius = 10
        //Cornerlabel.layer.masksToBounds = true
        //cell.addSubview(Cornerlabel)
        //cell.selectedBackgroundView = UIView.init(frame: cell.frame)
        //240,255,255
        //cell.selectedBackgroundView?.backgroundColor = UIColor.init(red: 0, green: 105/255, blue: 193/255, alpha: 1)
        
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if ChildType["name"]?.count != 0{
        for i in 0...(ChildType["name"]?.count)!-1
        {
            let index = IndexPath.init(item: i, section: 0)
            selectArr = nil
           //
           // collectionView.cellForItem(at: index)?.isSelected = false
            collectionView.deselectItem(at: index, animated: true)
            collectionView.cellForItem(at: index)!.backgroundColor = UIColor.white;
        }
        }
        self.selectArr = indexPath.row
        collectionView.reloadData()

        collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredVertically)
        //collectionView.cellForItem(at: indexPath)?.backgroundColor = UIColor.red
        //for  i in (collectionView.cellForItem(at: indexPath)?.contentView.subviews)!{
        //    i.tintColor = UIColor.white
       // }
        
            self.selectFlag = self.ChildType["id"]?[indexPath.row]
           self.Alarequest()
            //self.collectionView?.cellForItem(at: indexPath)?.backgroundColor = naviColor

      
        

    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
        self.selectFlag=nil
        self.selectArr = nil
        collectionView.cellForItem(at: indexPath)?.backgroundColor = UIColor.white

        collectionView.reloadData()
       Alarequest()

        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, shouldDeselectItemAt indexPath: IndexPath) -> Bool {
        
        return true
    }
    
    
    
    
    
    
}

